﻿using System;
using System.Collections.Generic;
using SizeboxUI;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace Sizebox.CharacterEditor
{
	// Token: 0x02000248 RID: 584
	public class MaterialEditView : BaseView
	{
		// Token: 0x1700017A RID: 378
		// (get) Token: 0x06000CA0 RID: 3232 RVA: 0x0006658B File Offset: 0x0006478B
		public Texture[] DetailTextures
		{
			get
			{
				return this.detailTextures;
			}
		}

		// Token: 0x06000CA1 RID: 3233 RVA: 0x00066594 File Offset: 0x00064794
		protected override void Awake()
		{
			base.Awake();
			Camera main = Camera.main;
			this.control = main.GetComponent<InterfaceControl>();
			this.placement = this.control.GetComponent<EditPlacement>();
			this.materialControls = UnityEngine.Object.Instantiate<MaterialControlGui>(this.materialControlPrefab, base.transform);
			this.materialControls.gameObject.SetActive(false);
			this.resetButton.onClick.AddListener(new UnityAction(this.OnResetAll));
		}

		// Token: 0x06000CA2 RID: 3234 RVA: 0x00066610 File Offset: 0x00064810
		protected override void OnEnable()
		{
			base.OnEnable();
			if (!this.control.selectedEntity)
			{
				return;
			}
			this.materialEdit = this.control.selectedEntity.GetComponent<MaterialEdit>();
			if (!this.materialEdit)
			{
				return;
			}
			foreach (MaterialWrapper materialWrapper in this.materialEdit.Materials)
			{
				materialWrapper.RegisterDetailTextures(this.detailTextures);
			}
			this.EnableMaterialEntries();
		}

		// Token: 0x06000CA3 RID: 3235 RVA: 0x000666C0 File Offset: 0x000648C0
		protected override void OnDisable()
		{
			base.OnDisable();
			this.DisableMaterialEntries();
		}

		// Token: 0x06000CA4 RID: 3236 RVA: 0x000666D0 File Offset: 0x000648D0
		private void EnableMaterialEntries()
		{
			if (this.materialEdit == null)
			{
				return;
			}
			this.DisableMaterialEntries();
			this.EnsureMaterialEntries(this.materialEdit.Materials);
			int num = 0;
			foreach (MaterialWrapper wrapper in this.materialEdit.Materials)
			{
				this.materialEntries[num].gameObject.SetActive(true);
				this.materialEntries[num].RegisterMaterial(wrapper, this);
				num++;
			}
		}

		// Token: 0x06000CA5 RID: 3237 RVA: 0x00066784 File Offset: 0x00064984
		private void DisableMaterialEntries()
		{
			foreach (MaterialEntryGui materialEntryGui in this.materialEntries)
			{
				materialEntryGui.gameObject.SetActive(false);
			}
		}

		// Token: 0x06000CA6 RID: 3238 RVA: 0x000667E8 File Offset: 0x000649E8
		private void EnsureMaterialEntries(ICollection<MaterialWrapper> materialWrappers)
		{
			int count = materialWrappers.Count;
			for (int i = this.materialEntries.Count; i < count; i++)
			{
				MaterialEntryGui materialEntryGui = UnityEngine.Object.Instantiate<MaterialEntryGui>(this.materialEntryPrefab, this.controlsGuiParent);
				this.materialEntries.Add(materialEntryGui);
				materialEntryGui.gameObject.SetActive(false);
			}
		}

		// Token: 0x06000CA7 RID: 3239 RVA: 0x00066842 File Offset: 0x00064A42
		public MaterialControlGui GetControls()
		{
			return this.materialControls;
		}

		// Token: 0x06000CA8 RID: 3240 RVA: 0x0006684C File Offset: 0x00064A4C
		private void OnResetAll()
		{
			if (!this.materialEdit)
			{
				return;
			}
			foreach (MaterialWrapper materialWrapper in this.materialEdit.Materials)
			{
				materialWrapper.ResetMaterial();
			}
		}

		// Token: 0x040011AF RID: 4527
		[SerializeField]
		[Header("Prefabs")]
		private MaterialEntryGui materialEntryPrefab;

		// Token: 0x040011B0 RID: 4528
		[SerializeField]
		private MaterialControlGui materialControlPrefab;

		// Token: 0x040011B1 RID: 4529
		[Header("Required References")]
		[SerializeField]
		private RectTransform controlsGuiParent;

		// Token: 0x040011B2 RID: 4530
		[SerializeField]
		private Button resetButton;

		// Token: 0x040011B3 RID: 4531
		[Space]
		[SerializeField]
		[Header("Detail Textures")]
		private Texture[] detailTextures;

		// Token: 0x040011B4 RID: 4532
		private MaterialEdit materialEdit;

		// Token: 0x040011B5 RID: 4533
		private List<MaterialEntryGui> materialEntries = new List<MaterialEntryGui>();

		// Token: 0x040011B6 RID: 4534
		private MaterialControlGui materialControls;

		// Token: 0x040011B7 RID: 4535
		private InterfaceControl control;

		// Token: 0x040011B8 RID: 4536
		private EditPlacement placement;
	}
}
